package com.example.design.openfeign;

import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.HashMap;

/**
 * FeignClient工厂
 * @author wangli
 * @version 1.0
 * @date 2023/9/19 12:30
 */
@Component
public class DynamicFeignClientFactory <T>{

    private FeignClientBuilder feignClientBuilder;

    private HashMap<String, T> clientMap = new HashMap<>();

    public DynamicFeignClientFactory(ApplicationContext appContext) {
        this.feignClientBuilder = new FeignClientBuilder(appContext);
    }

    public T getFeignClient(final Class<T> type, String serviceId) {
        if (!clientMap.containsKey(serviceId)){
            clientMap.put(serviceId,this.feignClientBuilder.forType(type, serviceId).build());
        }
        return clientMap.get(serviceId);
    }

}
